package com.spboot.exts.service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.commons.lang3.StringUtils;
import org.apache.solr.client.solrj.SolrClient;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrQuery.ORDER;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.client.solrj.response.UpdateResponse;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
import org.apache.solr.common.SolrInputDocument;
import org.apache.solr.common.util.NamedList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.spboot.exts.entity.News;
import com.spboot.exts.entity.util.PageInfo;
import com.spboot.exts.mapper.NewsMapper;

/**
 * solr查询业务
 * 
 * @author Administrator
 *
 */
@Service
public class NewsService {

	@Autowired
	NewsMapper newsMapper;

	@Autowired
	SolrClient solrClientl;

	public boolean addNews(News news) {
		//数据库
		int n = newsMapper.insert(news);
		
		//同步添加到solr
		if(n > 0){
//			SolrInputDocument doc = new SolrInputDocument();
//			doc.addField("id", news.getId());
//			doc.addField("title", news.getTitle());
//			doc.addField("content", news.getContent());
//			doc.addField("createTime", news.getCreateTime());
			try {
				UpdateResponse response = solrClientl.addBean("easybuy_core",news);
//				UpdateResponse response = solrClientl.add("easybuy_core", doc);
				//提交事物
				solrClientl.commit("easybuy_core");
				System.out.println(response.getStatus() == 0 ?"solr添加成功!":"solr添加失败!");
			} catch (SolrServerException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return n > 0;
	}

	public PageInfo<News> selectNews(int currpageNo, String title) {
		PageInfo<News> pageInfo = new PageInfo<News>(currpageNo, 15);

		// 查询条件
		SolrQuery query = new SolrQuery();
		if (!StringUtils.isBlank(title)) {
			query.add("q", "title:" + title.replace(" ", ","));
		} else {
			query.add("q", "*:*");
		}
		// 查询分页
		query.setStart(pageInfo.getStart().intValue());
		query.setRows(Integer.valueOf(pageInfo.getSize() + ""));
		// 查询高亮
		// 开启高亮
		query.setHighlight(true);
		query.setHighlightSimplePre("<span style='color:red'>");
		query.setHighlightSimplePost("</span>");
		// 设置高亮字段
		query.addHighlightField("title");

		// 排序
		// 默认时间倒叙
		query.addSort("createTime", ORDER.desc);
		query.addSort("id", ORDER.desc);

		try {
			List<News> newsList = new ArrayList<News>();
			// core
			QueryResponse response = solrClientl.query("easybuy_core", query);
			// 高亮结果
			Map<String, Map<String, List<String>>> highlighting = response.getHighlighting();
			// 查询结果
			SolrDocumentList documentList = response.getResults();
			long total = documentList.getNumFound();
			for (SolrDocument solrDocument : documentList) {
				String id = solrDocument.get("id").toString();
				String hltitle = "";
				// 存在对应id的高亮数据
				if (highlighting.containsKey(id)) {
					Map<String, List<String>> map = highlighting.get(id);
					if (map.containsKey("title") && map.get("title") != null) {
						// 高亮的标题
						hltitle = map.get("title").get(0);
					}
				}
				News temp = new News();
				temp.setId(Integer.parseInt(id));
				// 高亮标题不为空用高亮标题
				temp.setTitle(hltitle != "" ? hltitle : solrDocument.get("title").toString());
				temp.setContent(solrDocument.get("content").toString());
				temp.setCreateTime(solrDocument.get("createTime").toString());
				newsList.add(temp);
			}

			// 设置分页数据
			pageInfo.setTotal(total);
			pageInfo.setRecords(newsList);
		} catch (SolrServerException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return pageInfo;
	}
}
